package com.ability;

import java.util.HashMap;

import com.charactersheet.sheet.CharacterSheet;
import com.info.BonusInfo;
import com.info.BonusTypeEnum;

public class CharacterAbility {

	private static final HashMap<String, CharacterAbility> ABILITY_MAP = new HashMap<String, CharacterAbility>();
	
	
	public static CharacterAbility getCharacterAbility(String playerName){
		if (!ABILITY_MAP.containsKey(playerName)){
			return null;
		}
		return ABILITY_MAP.get(playerName);
	}
	
	public static CharacterAbility createCharacterAbility(CharacterSheet sheet, String playerName, int str, int dex, int con, int intel, int wis, int cha){
		return ABILITY_MAP.put(playerName, new CharacterAbility(sheet, str, dex, con, intel, wis, cha));
	}
	
	private int abilityScores[];
	private CharacterSheet sheet;
	
	private CharacterAbility(CharacterSheet sheet, int str, int dex, int con, int intel, int wis, int cha){		
		abilityScores = new int[AbilityEnum.LAST_ENUM.ordinal()];
		
		abilityScores[AbilityEnum.STRENGTH.ordinal()] = str;
		abilityScores[AbilityEnum.DEXTERITY.ordinal()] = dex;
		abilityScores[AbilityEnum.CONSTITUTION.ordinal()] = con;
		abilityScores[AbilityEnum.INTELLIGENCE.ordinal()] = intel;
		abilityScores[AbilityEnum.WISDOM.ordinal()] = wis;
		abilityScores[AbilityEnum.CHARISMA.ordinal()] = cha;
		this.sheet = sheet;
	}
	
	public int[] getAbilityScores(){
		return abilityScores;
	}
	
	public void increaseScore(AbilityEnum ability){
		if (ability.ordinal() >= 0 && ability.ordinal() < AbilityEnum.LAST_ENUM.ordinal()){
			abilityScores[ability.ordinal()]++;
		}
	}
	
	
	public int[] getAbilityModifiers(){
		int mod[] = new int[AbilityEnum.LAST_ENUM.ordinal()];
		for (AbilityEnum i : AbilityEnum.values()){
			if (i.equals(AbilityEnum.LAST_ENUM)){
				break;
			}
			mod[i.ordinal()] = getAbilityModifier(i);
		}
		return mod;
	}
	
	private int getAbilityModifier(AbilityEnum ability){
		BonusInfo bonus = new BonusInfo();
		BonusTypeEnum type = AbilityEnum.convertAbilityToBonus(ability);
		bonus.addBonus(sheet.getEquip().getTotalBonus(type, null));
		bonus.addBonus(sheet.getClasses().getTotalBonus(type, null));
		int total = bonus.getBonusTotal() + abilityScores[ability.ordinal()];
		return (int)Math.floor((total - 10) / 2);
	}

}
